package com.jr.service.impl;


import com.google.gson.Gson;
import com.jr.entity.User;
import com.jr.mapper.UserDao;
import com.jr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;


/**
 * 用户信息表
 *
 * @author Snoopy
 * @since 1.0.0 2022-12-31
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Override
    public User checkUser(User user) {
        return userDao.selectUser(user);
    }

    @Override
    public boolean registerUser(User user) {
        String password = user.getUpassword();
        user.setUpassword(null);
        if(userDao.selectUser(user)!=null)
            return false;
        user.setUpassword(password);
        return userDao.insertUser(user)==1;
    }

    @Override
    public boolean changeUserInfo(User user, HttpSession session) {
        User user1 = (User) session.getAttribute("user");
        String pwd = null;
        if(user1!=null){
            pwd = user1.getUpassword();
        }
        if(userDao.updateUser(user)==1){
            user.setUpassword(pwd);
            session.setAttribute("user",user);
            session.setAttribute("user1",new Gson().toJson(user));
            return true;
        }

        return false;
    }
}